package de.tu_darmstadt.gdi1.xcom.tests.testcases.students;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel3;

/**
 * 
 * Tests if medipacks are correctly processed
 * 
 * @author Lukas Schwoebel
 *
 */
public class MedipackTest {
	
	private XcomTestAdapterLevel3 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel3();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel3#healthPointsLeft()}.
	 */
	@Test
	public final void testMedipackHealthPoints() {
		
		String testLevelMedipack = TestLevel.testLevel.replaceAll("\\.\\.\\.\\.\\.\\.\\.\\.\\.", "....+....");
		
		testAdapter.loadLevelFromString(testLevelMedipack);
		
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("At the beginning, player at 4:6 should have 50 healthpoints:\n"+testLevelMedipack, 50, testAdapter.healthPointsLeft());
		
		// medipack should have 25 healthpoints
		testAdapter.rotateActivePlayer(true);
		testAdapter.performActionForActivePlayer();
		assertEquals("After picking up the medipack, player at 4:6 should have 75 healthpoints:\n"+testAdapter.returnGamestateAsSavegame(),
				75, testAdapter.healthPointsLeft());
		
		// test with several healthpoints for player
		
		// 25 --> 50
		testLevelMedipack = testLevelMedipack.replaceAll("//@;4;6;n;50;75;50;50;10", "//@;4;6;e;25;75;50;50;10");
		testAdapter.loadLevelFromString(testLevelMedipack);
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("At the beginning, player at 4:6 should have 25 healthpoints:\n"+testLevelMedipack, 25, testAdapter.healthPointsLeft());
		testAdapter.performActionForActivePlayer();
		assertEquals("After picking up the medipack, player at 4:6 should have 50 healthpoints:\n"+testAdapter.returnGamestateAsSavegame(),
				50, testAdapter.healthPointsLeft());
		
		// 1 --> 26
		testLevelMedipack = testLevelMedipack.replaceAll("//@;4;6;e;25;75;50;50;10", "//@;4;6;e;1;75;50;50;10");
		testAdapter.loadLevelFromString(testLevelMedipack);
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("At the beginning, player at 4:6 should have 1 healthpoints:\n"+testLevelMedipack, 1, testAdapter.healthPointsLeft());
		testAdapter.performActionForActivePlayer();
		assertEquals("After picking up the medipack, player at 4:6 should have 26 healthpoints:\n"+testAdapter.returnGamestateAsSavegame(),
				26, testAdapter.healthPointsLeft());
		
		// 100 --> 125
		testLevelMedipack = testLevelMedipack.replaceAll("//@;4;6;e;1;75;50;50;10", "//@;4;6;e;100;75;50;50;10");
		testAdapter.loadLevelFromString(testLevelMedipack);
		testAdapter.changeActivePlayer(4, 6);
		assertEquals("At the beginning, player at 4:6 should have 100 healthpoints:\n"+testLevelMedipack, 100, testAdapter.healthPointsLeft());
		testAdapter.performActionForActivePlayer();
		assertEquals("After picking up the medipack, player at 4:6 should have 125 healthpoints:\n"+testAdapter.returnGamestateAsSavegame(),
				125, testAdapter.healthPointsLeft());
	}
	
	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#ammoLeft()}.
	 */
	@Test
	public final void testMedipackPickup() {

		String testLevelMedipack = TestLevel.testLevel.replaceAll("\\.\\.\\.\\.\\.\\.\\.\\.\\.", "....+....");
		testLevelMedipack = testLevelMedipack.replaceAll("//@;4;6;n;50;75;50;50;10", "//@;4;6;e;50;75;50;50;10");
		
		testAdapter.loadLevelFromString(testLevelMedipack);
		
		assertEquals("On 5:6 there should be a medikit:\n"+testLevelMedipack, '+', testAdapter.elementOnPosition(5, 6));
		
		testAdapter.changeActivePlayer(4, 6);
		testAdapter.performActionForActivePlayer();
		
		// should have increased healthpoints and not decreased actionpoints
		assertEquals("Picking up a medipack should have increased healthpoints from 50 to 75", 75, testAdapter.healthPointsLeft());
		assertEquals("Picking up a medipack should not have decreased actionpoints", 75, testAdapter.actionPointsLeft());
		// player should not have moved
		assertEquals("With picking up a medipack the player should not move", '@', testAdapter.elementOnPosition(4, 6));
		assertEquals("With picking up a medipack the player should not move", '.', testAdapter.elementOnPosition(5, 6));
	}
}
